﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.Business;

namespace ForkCarRelationManager
{
    public partial class FormAddCar : Form
    {
        private int CARID;
        private bool isLookUpCar;
        CarBI carBI = new CarBI();
        private int clientId;
        public FormAddCar()
        {
            InitializeComponent();
            isLookUpCar = false;
        }
        //添加汽车
        public FormAddCar(int ClientId,String ClientName)
        {
            InitializeComponent();
            isLookUpCar = false;
            this.LBClientName.Text = ClientName;
            clientId = ClientId;
            //初始化业务员列表
            MaintorBI maintorBI=new MaintorBI();
            DataTable salers = maintorBI.findMaintorByClientId(ClientId);
            this.CBSalers.DataSource = salers;
            this.CBSalers.DisplayMember = "MaintorName";
            this.CBSalers.ValueMember = "ID";
            
        }
        //查看汽车详情
        public FormAddCar(int carId)
        {
            InitializeComponent();
            CARID = carId;
            this.initData();
            isLookUpCar = true;
        }

        private void initData()
        {
            
            DataTable carTable = new DataTable();
            carTable = carBI.getCarWithId(CARID);
            this.LBClientName.Text = carTable.Rows[0]["ClientName"].ToString();
            this.TBBrand.Text = carTable.Rows[0]["Brand"].ToString();
           int insource = Convert.ToInt32(carTable.Rows[0]["Insourcing"].ToString());
           if (insource == 0)
           {
               this.RBfix.Select();
           }
           else if (insource == 1)
           {
               this.RBbuy.Select();

           }
           else if (insource ==2 )
           {
               this.RBzu.Select();
                    
           }
           this.TBDate.Value = Convert.ToDateTime(carTable.Rows[0]["BuyData"].ToString());
           this.TBType.Text = carTable.Rows[0]["CarType"].ToString();
           this.TBDeviceNum.Text = carTable.Rows[0]["DeviceNum"].ToString();
           this.TBEngineType.Text = carTable.Rows[0]["EngineType"].ToString();
           this.TBModel.Text = carTable.Rows[0]["Model"].ToString();
           this.TBSerialNum.Text = carTable.Rows[0]["SerialNum"].ToString();
           this.TBWorkHour.Text = carTable.Rows[0]["WorkHour"].ToString();
           this.TBConfigure.Text = carTable.Rows[0]["Configure"].ToString();
           this.IntPutTotleNum.Text = carTable.Rows[0]["SumHour"].ToString();
           this.CBSalers.Text = carTable.Rows[0]["BusinessMan"].ToString();
           this.CBSalers.Enabled = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        //取消按钮
        private void button1_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        //保存按钮（可执行更新和添加功能）
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.TBDeviceNum.Text.Trim() != "")
            {
                int servceType = 0;
                if (RBfix.Checked)
                {
                    servceType = 0;
                }
                if (RBbuy.Checked)
                {
                    servceType = 1;
                }
                if (RBzu.Checked)
                {
                    servceType = 2;
                }
                if (isLookUpCar)
                {
                    //修改
                    bool mark = carBI.updateCarInfo(CARID, this.TBBrand.Text, this.TBType.Text, this.TBModel.Text, this.TBDeviceNum.Text, this.TBSerialNum.Text, this.TBEngineType.Text, servceType, this.TBConfigure.Text, this.TBDate.Value.ToString(), Convert.ToInt32(this.TBWorkHour.Text), Convert.ToInt32(IntPutTotleNum.Text), this.CBSalers.Text);
                    if (mark)
                    {
                        MessageBox.Show("修改成功！");
                    }
                    else
                    {
                        MessageBox.Show("修改失败！");
                    }
                }
                else
                {
                    //保存
                    bool mark = carBI.addCarInfo(clientId, this.TBBrand.Text, this.TBType.Text, this.TBModel.Text, this.TBDeviceNum.Text, this.TBSerialNum.Text, this.TBEngineType.Text, servceType, this.TBConfigure.Text, this.TBDate.Value.ToString(), Convert.ToInt32(this.TBWorkHour.Text), Convert.ToInt32(IntPutTotleNum.Text), this.CBSalers.Text);
                    if (mark)
                    {
                        MessageBox.Show("添加成功！");
                        this.DialogResult = DialogResult.Yes;
                    }
                    else
                        MessageBox.Show("添加失败！");
                }
            }
            else
                MessageBox.Show("机号不能为空");
        }

        private void FormAddCar_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }

    }
}
